package myjava.algorithm.leetcode;

public class LongestCommonPrefix {
	public static String longestCommonPrefix(String[] strs) {
		String result = "";
		
		if(strs == null || strs.length == 0) return result;
		
		if(strs.length == 1) return strs[0];
		
		int min = Integer.MAX_VALUE;
		for(int i=0;i<strs.length;i++)
		{
			if(min > strs[i].length()) min = strs[i].length();
		}
		
		if(min == 0) return "";
		
		char c;
		StringBuilder sb = new StringBuilder();
Tag:
		for(int k=0;k<min;k++)
		{
			c = strs[0].charAt(k);
			for(int i=0;i<strs.length;i++)
			{
				if(strs[i].charAt(k) != c)
				{
					break Tag;
				}
			}
			
			sb.append(c);
		}

		return sb.toString();
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] strs = new String[2];
		strs[0]= "a";
		strs[1]= "b";
		
		System.out.println(longestCommonPrefix(strs));
	}

}
